package com.jmm.neditor.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private final static String sdfYear = "yyyy";

	private final static String sdfDay = "yyyy-MM-dd";
	
	private final static String sdfDays = "yyyyMMdd";

	private final static String sdfTime = "yyyy-MM-dd HH:mm:ss";
	private final static String sdfTimes = "yyyyMMddHHmmssSSS";
	/**
	 * 获取YYYY格式
	 * 
	 * @return
	 */
	public static String getYear() {
		SimpleDateFormat format=new SimpleDateFormat(sdfYear);
		return format.format(new Date());
	}
	/**
	 * 获取int类型year
	 * @return
	 */
	public static int getYearInt() {
        SimpleDateFormat format=new SimpleDateFormat(sdfYear);
        String yearStr = format.format(new Date());
        int year = Integer.parseInt(yearStr);
        return year;
    }
	
	/**
	 * 获取int类型day
	 * @return
	 */
	public static int getDayInt(){
	    SimpleDateFormat format=new SimpleDateFormat("dd");
        String yearStr = format.format(new Date());
        int year = Integer.parseInt(yearStr);
        return year;
	}
	
	/**
	 * 获取int类型month
	 * @return
	 */
	public static int getMonthInt(){
	    SimpleDateFormat format=new SimpleDateFormat("MM");
        String yearStr = format.format(new Date());
        int year = Integer.parseInt(yearStr);
        return year;
	}
	/**
	 * 获取YYYY-MM-DD格式
	 * 
	 * @return
	 */
	public static String getDay() {
		SimpleDateFormat format=new SimpleDateFormat(sdfDay);
		return format.format(new Date());
	}
	
	/**
	 * 获取YYYYMMDD格式
	 * 
	 * @return
	 */
	public static String getDays(){
		SimpleDateFormat format=new SimpleDateFormat(sdfDays);
		return format.format(new Date());
	}
	/**
	 * 获取yyyymmddhhmmssSSS
	 * @return
	 */
	public synchronized static String getTimes(){
		SimpleDateFormat format=new SimpleDateFormat(sdfTimes);
		String str = format.format(new Date());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            return str;
        }
        return str;
	}
	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * 
	 * @return
	 */
	public static String getTime() {
		SimpleDateFormat format=new SimpleDateFormat(sdfTime);
		return format.format(new Date());
	}
	
	/**
	 * 时间戳(加锁，用于订单的获取，防止保证订单号的唯一)
	 */
	public synchronized static String gettime(){
		String str = String.valueOf(Calendar.getInstance().getTime().getTime());
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return str;
		}
		return str;
	}

	/**
	* @Title: compareDate
	* @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	* @param s
	* @param e
	* @return boolean  
	* @throws
	* @author luguosui
	 */
	public static boolean compareDate(String s, String e) {
		if(fomatDate(s)==null||fomatDate(e)==null){
			return false;
		}
		return fomatDate(s).getTime() >=fomatDate(e).getTime();
	}

	/**
	 * 格式化日期
	 * 
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 校验日期是否合法
	 * 
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return false;
		}
	}
	public static int getDiffYear(String startTime,String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			long aa=0;
			int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}
	  /**
     * <li>功能描述：时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long 
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Date endDate = new Date();
        
            try {
				beginDate = format.parse(beginDateStr);
				endDate= format.parse(endDateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
      
        return day;
    }
    
    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
    	int daysInt = Integer.parseInt(days);
    	
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        
        return dateStr;
    }
    
    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
    	int daysInt = Integer.parseInt(days);
    	
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        
        return dateStr;
    }
    
    public static void main(String[] args) {
    	
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++)
				{
				}
			}
		}).start();
    	
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++)
				{
				}
			}
		}).start();
    	
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<5;i++)
				{
				}
			}
		}).start();
    	
    }
	/**
	 * 格式化字符串，解决从MySql中查出来的数据后面多了0的问题
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	/**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 格式化时间
     * 
     * @return
     */
    public static Date fomatDateTime(String time) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return fmt.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
	public static String fomatDateTimeStr(String time) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.format(Timestamp.valueOf(time));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

    public static boolean compareDateTime(String s, String e) {
        if(fomatDateTime(s)==null||fomatDateTime(e)==null){
            return false;
        }
        return fomatDateTime(s).getTime() >=fomatDateTime(e).getTime();
    }
    //两个日期距离的天数
    public static int getDaysBetween(Date dtBegin, Date dtEnd) {
    	return new Long((dtBegin.getTime() - dtEnd.getTime()) / 86400000).intValue();
    }
    //两个日期距离的分钟数
    public static int minuteBetween(Date smdate,Date bdate)  {
        long between_days=(bdate.getTime()-smdate.getTime())/(1000*60);
        System.out.println(between_days);
        return Integer.parseInt(String.valueOf(between_days));
    }
    
    /*
    	获取当前时间之前或之后几分钟 minute
    */
    public static String getTimeByMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }
    
    /*
    	获取当前时间之前或之后几小时 hour
     */
    public static String getTimeByHour(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }
    
}
